package top.lshaci.learning.nio;

import cn.hutool.core.util.StrUtil;

import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * NioChannel01
 *
 * @author lshaci
 * @since 1.0.0
 */
public class NioFileChannelWrite {

    public static void main(String[] args) throws Exception {
        String str = "Hello, NIO; 你好";

        FileOutputStream fos = new FileOutputStream("E:/NIO1.txt");
        // 通过 FileOutputStream 获取 FileChannel
        // 这个 FileChannel 的实际类型是 FileChannelImpl
        final FileChannel channel = fos.getChannel();

        // 创建一个缓冲区buffer
        final ByteBuffer byteBuffer = StrUtil.byteBuffer(str, "UTF-8");

        // 对 buffer 进行反转
//        byteBuffer.flip();

        // 把 buffer 中的数据写入到 channel中
        channel.write(byteBuffer);

        fos.close();
    }
}
